#coding=utf8
#LightCam 交易数据快照程序
#init by hfh 20151027

import time,re,sys,traceback,threading,subprocess,pymongo,json,os
import lc_libs

if __name__ == "__main__":


	lchome=lc_libs.lc_getlchome()
	scname=sys.argv[0].split('.')[0]
	logfilename=lchome+'/log/'+scname+'.log'
	confname=lchome+'/conf/'+scname+'.conf'
	
	today=time.strftime('%Y%m%d',time.localtime())
	os.system("mv %s %s.%s" % (logfilename,logfilename,today))
	
	logfile=file(logfilename,'a')
	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	logfile.write(logdate+" Started.\n")
	logfile.flush()
	
	#try:
	#	#初始化数据库
	#	db=lc_libs.lc_mongodb_operater('127.0.0.1',27017)
  #
	#	db.connect()
	#	Conn=db.getConn()
	#	if Conn==None:
	#		print "Can not Connect to the MondbDB"
	#		exit(0)
	#	
	#	perf_dbname='lc_perf'
	#	vipport_tablename='lc_vipport'
	#	
	#	perf_db=db.useDatabase(perf_dbname)
	#	vipport_table=db.UseCollection(perf_db,vipport_tablename)

	#except Exception:
	#	excinfo=traceback.format_exc()
	#	logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
	#	logfile.write(logdate+" 初始化数据库失败:\n"+excinfo+"\n")
	#	logfile.flush()
	#	exit(0)

	#timelist=['07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00']
	
	############### init timelist ##############################
	starttime=time.mktime(time.strptime('07','%H'))
	endtime=time.mktime(time.strptime('17','%H'))
	
	thistime=starttime
	timelist=[]
	i=3600
	while True:
		thistimestr=time.strftime('%H',time.localtime(thistime))
		timelist.append(thistimestr)
		if thistime==endtime:
			break
		else:
			time.sleep(0.01)
		thistime=thistime+i
		
	############################################
	
		
	while True:
		time.sleep(1)
		try:
			
			if time.localtime()[6] > 4:
				time.sleep(3600)
				continue
			
			today=time.strftime('%Y%m%d',time.localtime())
			now=time.strftime('%H',time.localtime())
			
			if now not in timelist:
				time.sleep(60)
				continue
			
			seatinfo_filename="%s/pyweb/static/json/tradedata/history/seatinfo%s%s.json" % (lchome,today,now)
			seatinfototal_filename="%s/pyweb/static/json/tradedata/history/seatinfototal%s%s.txt" % (lchome,today,now)
			mdkseat_filename="%s/pyweb/static/json/tradedata/history/mdkseat%s%s.json" % (lchome,today,now)
			mdkseattotal_filename="%s/pyweb/static/json/tradedata/history/mdkseattotal%s%s.txt" % (lchome,today,now)
			devicesum_filename="%s/pyweb/static/json/network/history/devicesum%s%s.json" % (lchome,today,now)
			
			seatinfo_cmd="cp %s/pyweb/static/json/tradedata/seatinfo.json %s" % (lchome,seatinfo_filename)
			seatinfototal_cmd="cp %s/pyweb/static/json/tradedata/seatinfototal.txt %s" % (lchome,seatinfototal_filename)
			mdkseat_cmd="cp %s/pyweb/static/json/tradedata/mdkseat.json %s" % (lchome,mdkseat_filename)
			mdkseattotal_cmd="cp %s/pyweb/static/json/tradedata/mdkseattotal.txt %s" % (lchome,mdkseattotal_filename)
			devicesum_cmd="cp %s/pyweb/static/json/network/devicesum.json %s" % (lchome,devicesum_filename)
			
			if os.path.exists(seatinfo_filename)==False:
				os.system(seatinfo_cmd)
			if os.path.exists(seatinfototal_filename)==False:
				os.system(seatinfototal_cmd)
			if os.path.exists(mdkseat_filename)==False:
				os.system(mdkseat_cmd)
			if os.path.exists(mdkseattotal_filename)==False:
				os.system(mdkseattotal_cmd)
			if os.path.exists(devicesum_filename)==False:
				os.system(devicesum_cmd)
			#########################################
		except Exception:
			excinfo=traceback.format_exc()
			logdate=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())
			logfile.write(logdate+" \n"+excinfo+"\n")
			logfile.flush()
		
		time.sleep(60)
